home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / c / library / dos / diverses / cexpress / strings / seekstrg.asm < prev    next >
Encoding:
Assembly Source File  |  1989-05-03  |  4.1 KB  |  143 lines

  1. ;unsigned short  seek_string(strg,sub_strg,start_pt);
  2. ;  char  *strg,*sub_str;
  3. ;  unsigned short  start_pt;
  4.  
  5.     EXTRN  _memory_model:byte
  6.     EXTRN  _error_code:byte
  7.  
  8. _TEXT    SEGMENT BYTE PUBLIC 'CODE'
  9.     ASSUME CS:_TEXT
  10.     PUBLIC _seek_string
  11. _seek_string proc near
  12.     push bp            ;
  13.     mov  bp,sp        ;set up stack frame
  14.     push di            ;
  15.     push si            ;
  16.     push ds            ;
  17.     cmp  _memory_model,0    ;near or far?
  18.     jle  begin        ;jump if near
  19.     inc  bp            ;else add 2 to BP
  20.     inc  bp            ;
  21. begin:    cmp  _memory_model,2    ;data near or far?
  22.     jb   A1            ;jump if near
  23.     lds  si,dword ptr[bp+4] ;DS:SI pts to Strg
  24.     les  di,dword ptr[bp+8] ;DS:DI pts to SubStrg
  25.     mov  cx,[bp+12]        ;get start_pt
  26.     jmp  short B1        ;
  27. A1:    mov  si,[bp+4]        ;NEAR case
  28.     mov  di,[bp+6]        ;
  29.     mov  cx,[bp+8]        ;
  30.     mov  ax,ds        ;ES = DS
  31.     mov  es,ax        ;
  32. B1:    push di            ;figure substring length
  33.     sub  dl,dl        ;count in dl
  34. C1:    cmp  byte ptr es:[di],0    ;null yet?
  35.     je   D1            ;jump if so
  36.     inc  di            ;forward ptr
  37.     inc  dl            ;inc counter
  38.     jmp  short C1        ;loop
  39. D1:    pop  di            ;
  40.     mov  bp,3        ;error_code 3 = sub_strg is null
  41.     or   dl,dl        ;sub_strg length non-zero?
  42.     jnz  F1            ;continue if so
  43. E1:    jmp  R1            ;error: quit routine
  44. F1:    push si            ;figure Strg length
  45.     sub  dh,dh        ;count in dh
  46. G1:    cmp  byte ptr[si],0    ;null yet?
  47.     je   H1            ;quit if so
  48.     inc  si            ;forward ptr
  49.     inc  dh            ;inc counter
  50.     jmp  short G1        ;loop
  51. H1:    pop  si            ;
  52.     dec  bp            ;2 = Strg is null
  53.     or   dh,dh        ;test for null
  54.     jz   E1            ;quit if null
  55.     mov  bl,cl        ;start_pt
  56.     add  bl,dl        ;sub_strg length
  57.     cmp  bl,dh        ;compare to strg length
  58.     ja   E1            ;quit if out of range
  59.     mov  bp,1        ;error_code 1 = not found
  60.     mov  bl,cl        ;start_pt to pos counter
  61.     dec  bl            ;adjust
  62.     sub  dh,cl        ;sub from search len
  63.     inc  dh            ;adjust
  64.     add  si,cx        ;begin search at start_pt
  65. I1:    mov  al,es:[di]        ;get char from sub_strg
  66.     mov  ah,al        ;copy in AH
  67.     cmp  al,97        ;low end of lower case
  68.     jb   J1            ;jump if below
  69.     cmp  al,122        ;upper end of lower case
  70.     ja   K1            ;neither upper nor lower
  71.     sub  ah,32        ;make AH upper case
  72.     jmp  short K1        ;go test both
  73. J1:    cmp  al,65        ;low end of upper case
  74.     jb   K1            ;neither upper nor lower
  75.     cmp  al,90        ;high end of upper case
  76.     ja   K1            ;neither upper nor lower
  77.     add  ah,32        ;make AH lower case
  78. K1:    inc  bl            ;inc Strg pos counter
  79.     or   dh,dh        ;counter = zero?
  80.     jz   R1            ;quit if so
  81.     dec  dh            ;dec length counter
  82.     mov  bh,[si]        ;get char from Strg
  83.     cmp  bh,al        ;test for match
  84.     je   L1            ;jump if found
  85.     cmp  bh,ah        ;2nd test for match
  86.     je   L1            ;jump if found
  87.     inc  si            ;forward Strg ptr
  88.     jmp  short K1        ;go check next char
  89. L1:    cmp  dl,1        ;single char substring?
  90.     jne  M1            ;jump if not
  91.     mov  bp,0        ;set error_code
  92.     jmp  short S1        ;go quit
  93. M1:    sub  cx,cx        ;clear CX
  94.     mov  cl,dl        ;CX = Substr length
  95.     dec  cl            ;1st char already matched
  96.     push si            ;save Strg position
  97.     push di            ;save Substr position
  98. N1:    inc  si            ;pt to nxt char of Strg
  99.     inc  di            ;pt to nxt char of Substr
  100.     mov  al,es:[di]        ;get char from of Substr
  101.     mov  ah,al        ;copy in AH
  102.     cmp  al,97        ;low end of lower case
  103.     jb   O1            ;jump if below
  104.     cmp  al,122        ;upper end of lower case
  105.     ja   P1            ;neither upper nor lower
  106.     sub  ah,32        ;make AH upper case
  107.     jmp  short P1        ;go test both
  108. O1:    cmp  al,65        ;low end of upper case
  109.     jb   P1            ;neither upper nor lower
  110.     cmp  al,90        ;high end of upper case
  111.     ja   P1            ;neither upper nor lower
  112.     add  ah,32        ;make AH lower case
  113. P1:    mov  bh,[si]        ;get char from Strg
  114.     cmp  bh,al        ;test for match
  115.     je   Q1            ;jump if found
  116.     cmp  bh,ah        ;2nd test for match
  117.     je   Q1            ;jump if found
  118.     pop  di            ;match not made
  119.     pop  si            ;restore prior ptrs
  120.     inc  si            ;forward Strg ptr
  121.     jmp  short I1        ;resume search for 1st ch
  122. Q1:    loop N1            ;go compare next char
  123.     pop  di            ;substring found!
  124.     pop  si            ;balance stack
  125.     dec  bp            ;error_code 0 = no error
  126.     jmp  short S1        ;go set return value
  127. R1:    mov  bl,0        ;return 0
  128. S1:    sub  bh,bh        ;return value in BL, Clear BH
  129.     mov  ax,bx        ;set return value
  130.     pop  ds            ;restore DS
  131.     mov  bx,bp        ;get return value
  132.     mov  _error_code,bl    ;set _error_code
  133.     pop  si            ;
  134.     pop  di            ;
  135.     pop  bp            ;
  136.     cmp  _memory_model,0    ;quit
  137.     jle  quit        ;
  138.     db   0CBh        ;RET far
  139. quit:    ret            ;RET near
  140. _seek_string ENDP
  141. _TEXT    ENDS
  142.     END
  143.